import { queryNearbyShopList } from "@src/api/reservation";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import base from "@src/store/base";
import location from "@src/store/location";
import {
  OPEN_STATE,
  OPEN_STATE_TEXT,
} from "@src/common/reservation/constant/index.js";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [
    {
      store: base,
      fields: ["themeColor", "themeTitleColor"],
    },
    {
      store: location,
      fields: ["latitude", "longitude"],
      actions: ["getLocation"],
    },
  ],
  data: {
    isInputFocus: false,
    inputValue: "",
    OPEN_STATE,
    OPEN_STATE_TEXT,
  },
  methods: {
    //下拉刷新
    onPullDownRefresh() {
      wx.showLoading({ title: "加载中" });
      this.queryNearbyShopListReq();
    },
    onLoad() {
      wx.nextTick(() => {
        this.queryNearbyShopListReq();
      });
    },
    inputFocus() {
      this.setData({ isInputFocus: true });
    },
    inputBlur() {
      this.setData({ isInputFocus: false });
      this.queryNearbyShopListReq();
    },
    bindKeyInput: function (e) {
      this.setData({
        inputValue: e.detail.value,
      });
    },
    chooseItem(e) {
      // let shopId = e.currentTarget.dataset.shopid;
      // let shopConfigId = e.currentTarget.dataset.shopconfigid;
      // let status = e.currentTarget.dataset.status;
      const { shopid, shopconfigid, status } = e.currentTarget.dataset;
      if (status !== OPEN_STATE.ON) {
      // if (status !== OPEN_STATE.ON && status !== OPEN_STATE.REST) {
        wx.showToast({
          title: `该门店${OPEN_STATE_TEXT[status]}`,
          icon: "none",
        });
        return;
      }
      const eventChannel = this.getOpenerEventChannel();
      if (eventChannel) {
        eventChannel.emit("changeShop", {
          shopId: shopid,
          shopConfigId: shopconfigid,
        });
      }
      wx.navigateBack();
    },
    clearInputValue() {
      this.setData({ inputValue: "" });
    },
    queryNearbyShopListReq() {
      let data = {
        shopName: this.data.inputValue,
        latitude: this.data.latitude,
        longitude: this.data.longitude,
      };
      return queryNearbyShopList(data).then((res) => {
        if (res.code === 0) {
          setTimeout(() => {
            wx.hideLoading();
            wx.stopPullDownRefresh();
            this.setData({ nearbyShopList: res.data });
          }, 500);
        }
      });
    },
  },
});
